﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Citrix.Cmp;
using System.ComponentModel;

namespace Citrix.XaAdminMobile.ViewModel
{
    /// <summary>
    /// The v-m for the master navigation toolbar show on multiple pages.
    /// </summary>
    internal class NavToolbarViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// A reference back to the root v-m object.
        /// </summary>
        private readonly ViewModelManager vmRoot;

        /// <summary>
        /// Initialisation ctor.
        /// </summary>
        /// <param name="vmMgr">The root v-m object.</param>
        /// <exception cref="ArgumentNullException">vmMgr</exception>
        public NavToolbarViewModel(ViewModelManager vmMgr)
        {
            if (null == vmMgr)
            {
                throw new ArgumentNullException("vmMgr");
            }

            this.vmRoot = vmMgr;
            this.PowerCommand = new DelegateCommand(() => this.vmRoot.App.Shutdown());
            this.SettingsCommand = new DelegateCommand(() => this.vmRoot.NavigateToPage(this.vmRoot.SettingsViewModel));
            this.ServersCommand = new DelegateCommand(() => this.vmRoot.NavigateToPage(this.vmRoot.ServersViewModel));
            this.SessionsCommand = new DelegateCommand(() => this.vmRoot.NavigateToPage(this.vmRoot.SessionsViewModel));
            this.vmRoot.PageChanged += new EventHandler(VmRoot_PageChanged);
        }

        /// <summary>
        /// Whether the servers page active. Used to show a highlight on the associated button.
        /// </summary>
        public bool IsServerPageActive
        {
            get { return this.vmRoot.ActiveNavigation.IsActive(this.vmRoot.ServersViewModel); }
        }

        /// <summary>
        /// Whether the sessions page active. Used to show a highlight on the associated button.
        /// </summary>
        public bool IsSessionsPageActive
        {
            get { return this.vmRoot.ActiveNavigation.IsActive(this.vmRoot.SessionsViewModel); }
        }

        /// <summary>
        /// Whether the settings page active. Used to show a highlight on the associated button.
        /// </summary>
        public bool IsSettingsPageActive
        {
            get { return this.vmRoot.ActiveNavigation.IsActive(this.vmRoot.SettingsViewModel); }
        }

        /// <summary>
        /// The power command object to shut the app down.
        /// </summary>
        public DelegateCommand PowerCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// The servers command displays the servers page.
        /// </summary>
        public DelegateCommand ServersCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// The sessions command displays the sessions page.
        /// </summary>
        public DelegateCommand SessionsCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// The settings command displays the settings page.
        /// </summary>
        public DelegateCommand SettingsCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// Page changed handler - used to trigger page active events to update the UI.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VmRoot_PageChanged(object sender, EventArgs e)
        {
            OnPropertyChanged("IsServerPageActive");
            OnPropertyChanged("IsSessionsPageActive");
            OnPropertyChanged("IsSettingsPageActive");
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Fired whenever a public property changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Fires the property change event.
        /// </summary>
        /// <param name="propertyName">The name of the property that changed.</param>
        private void OnPropertyChanged(string propertyName)
        {
            NotifyHelper.VerifyProperty(propertyName, this);
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion // INotifyPropertyChanged Members
    }
}
